package com.yida.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yida.bean.blog;
import com.yida.bean.blogger;
import com.yida.bean.tag;
import com.yida.service.serverblog;

@Controller
@RequestMapping("blog")
public class index {

	@Autowired
	public serverblog service;
	
	@RequestMapping("/index")
	public String index(Model mode,Integer page,Integer id,HttpServletRequest request) {
	    Integer pages = 1;
		if (page!=null) {
			pages=page;
		}
		if (id==null) {
		  //按时间排序的博客
		  List<blog> blog= service.selectblog(pages);
		  //查询总数
		  Integer pageall = service.selectBlogsSumservice();
		  mode.addAttribute("bloglist", blog);
		  mode.addAttribute("pages", pageall);
		}else {
		  //按标签+时间查询博客
		  List<blog> blog = service.selecttagBlogservice(pages,id);
		 //查询标签下博客总数
		  Integer pageall = service.selecttagBlogsumservice(id);
		  mode.addAttribute("bloglist", blog);
		  mode.addAttribute("pages", pageall);
		  mode.addAttribute("tagselectid", id);
		}
	
		
		//博主
		blogger blogger = service.selectBloggerindexservice();
		//热门博客
		List<blog> blogre = service.selectblogreservice();
		//查询日志
	    List<com.yida.bean.log> log = service.selectlogservice(1);
	   //查询标签
	    List<tag> tags = service.selecttagservice();
	  
	    
	    
		
		mode.addAttribute("cpage", pages);
		mode.addAttribute("blogger", blogger);
		mode.addAttribute("tags", tags);
		mode.addAttribute("bloglistre", blogre);
		mode.addAttribute("loglist", log);
		
	//	if (request.getHeader("x-forwarded-for") == null) {
		//	System.out.println(request.getRemoteAddr());
		//	}
	//	else {
	//		System.out.println( request.getHeader("x-forwarded-for"));
	//	}
			
			
		return "index";
	}
	
	
	


	@RequestMapping("/logmore")
	@ResponseBody
	public Object log(Model mode,Integer page) {
	    Integer pages = 1;
		if (page!=null) {
			pages=page;
		}
		//查询日志
	    List<com.yida.bean.log> log = service.selectlogservice(pages);
	    mode.addAttribute("logpage",pages);
		return log;
	}
	
	
}
